<template>
  <div class="edit-popup">
      <popup
          ref="popupRef"
          :title="popupTitle"
          :async="true"
          width="550px"
          @confirm="handleSubmit"
          @close="handleClose"
      >
          <el-form
              class="ls-form"
              ref="formRef"
              :rules="rules"
              :model="formData"
              label-width="60px"
          >
              <el-form-item label="名称" prop="name">
                  <el-input
                      class="ls-input"
                      v-model="formData.name"
                      placeholder="请输入名称"
                      clearable
                  />
              </el-form-item>
              <el-form-item label="备注" prop="desc">
                  <el-input
                      v-model="formData.remark"
                      type="textarea"
                      :autosize="{ minRows: 4, maxRows: 6 }"
                      placeholder="请输入备注"
                      maxlength="200"
                      show-word-limit
                  />
              </el-form-item>
              <el-form-item label="排序" prop="sort">
                  <el-input-number v-model="formData.sort" :min="0" :max="9999" />
              </el-form-item>
          </el-form>
      </popup>
  </div>
</template>
<script setup> 
import { editRole, addRole } from '@/apis/system/role'
import Popup from '@/components/popup/index.vue'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef()
const popupRef = shallowRef()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑角色' : '新增角色'
})
const formData = reactive({
  id: '',
  name: '',
  remark: '',
  sort: 0,
  menu_id: []
})

const rules = {
  name: [
      {
          required: true,
          message: '请输入名称',
          trigger: ['blur']
      }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  mode.value == 'edit' ? await editRole(formData) : await addRole(formData)
  popupRef.value?.close()
  emit('success')
}

const handleClose = () => {
  emit('close')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data) => {
  for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
          //@ts-ignore
          formData[key] = data[key]
      }
  }
}

defineExpose({
  open,
  setFormData
})
</script>
